﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LL.Business;
using LL.Utility.WindowsForms;
using NanoERP.Business.Permission;
using NanoERP.Model.Poco;
using NanoERP.Infrastructure;
using NanoERP.Business;
using NanoERP.Model.SystemMgmt;

namespace NanoERP.Modules.SystemMgmt
{
    //[Module("RoleMgr")]
    public partial class FrmSelectRole : EditFormBase
    {
        public FrmSelectRole()
        {
            InitializeComponent();
            InitControls();
            //LoadData();
        }

        private void InitControls()
        {
            InitTreeView();
            //ChangeUiState();
        }

        private void InitTreeView()
        {

            var tvHelper = new TreeViewHelper(this.treeView1);
            tvHelper.EnableCheckAll();
        }

        public SYS_User Data { get; set; }

        protected override void LoadData()
        {
            if (Data == null)
                return;
            RoleTree tree = null;
            using (var session =BusinessSessionFactory.NewSession())
            {
                var srv = session.Get<RoleService>();
                tree = srv.GetRoleTreeForUser(Data.Id);
            }
            BuildNode(treeView1.Nodes[0].Nodes, tree.Children);
            BuildRoles(treeView1.Nodes[0].Nodes, tree);
            if (this.treeView1.Nodes[0] != null)
                this.treeView1.Nodes[0].ExpandAll();
        }

        private void BuildNode(TreeNodeCollection nodes,
            IEnumerable<RoleTree> roleTypes)
        {
            foreach (var type in roleTypes)
            {
                var node = new TreeNode(type.Name)
                {
                    Tag = type
                };
                BuildNode(node.Nodes, type.Children);
                BuildRoles(node.Nodes, type);
                nodes.Add(node);
            }
        }

        private static void BuildRoles(TreeNodeCollection nodes,
            RoleTree tree)
        {
            foreach (var role in tree.Roles)
            {
                nodes.Add(new TreeNode(role.Name)
                {
                    Checked = role.Checked,
                    Tag = role,
                    ImageKey = "group_blue.png",
                    SelectedImageKey = "group_blue.png"
                });
            }
        }

        protected override bool SaveData()
        {
            var list = new List<Guid>();
            GetSelectValue(list, treeView1.Nodes[0].Nodes);
            using (var session =BusinessSessionFactory.NewSession(true))
            {
                session.Get<UserService>().SetRoles(Data.Id, list);
                session.Commit();
            }
            return true;
        }

        /// <summary>
        /// 递归获取所有选中角色Id
        /// </summary>
        private void GetSelectValue(ICollection<Guid> list,
            TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                var item = node.Tag as SYS_Role;
                if (item == null)
                {
                    GetSelectValue(list, node.Nodes);
                    continue;
                }
                if (node.Checked)
                    list.Add(item.Id);
            }
        }

    }
}
